package com.normal.main.activity

import android.Manifest
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.didi.drouter.annotation.Router
import com.normal.main.R
import com.normal.main.databinding.ActivityMvvmbindBinding
import com.normal.main.vm.MainActVM
import com.normal.zbase.impl.arouter.DRouterPath
import com.normal.zbase.impl.base.BaseActivity
import com.normal.zbase.impl.base.BaseBean
import com.normal.zbase.impl.base.BaseRecyclerViewAdapter
import com.normal.zbase.impl.event.EventBean
import com.normal.zbase.impl.event.EventUtils
import com.normal.zbase.impl.manager.PermissionManager.reqeustPermission
import com.normal.zbase.impl.tools.extend.colorRes
import com.normal.zbase.impl.tools.extend.logger
import com.normal.zbase.impl.tools.obj.ToolbarFactory
import com.normal.zbase.impl.tools.utils.ApplicationUtils
import com.normal.zbase.impl.tools.utils.JsonUtils
import com.normal.zbase.impl.tools.utils.TToast
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@Router(path = DRouterPath.Path.APP_MVVM)
class MVVMBindActivity : BaseActivity<ActivityMvvmbindBinding>() {
    override fun viewBinding(): ActivityMvvmbindBinding {
        return ActivityMvvmbindBinding.inflate(layoutInflater)
    }

    val a:String get() = ""
    val b by lazy {
        ""
    }

    private val mainActVM by viewModels<MainActVM>()
    private val adapter = BaseRecyclerViewAdapter<String>(R.layout.adapter_item)
    private val list = mutableListOf(
        "o1",
        "o2",
        "o3",
        "o4",
        "o5",
        "o6",
        "o7",
        "o8",
        "o9",
        "o1",
        "o2",
        "o3",
        "o4",
        "o5",
        "o6",
        "o7",
        "o8",
        "o9"
    )



    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        //获取权限
        permissionRequest()

        with(viewBinding) {
            activity = this@MVVMBindActivity
            mRecyclerView.layoutManager = LinearLayoutManager(this@MVVMBindActivity)
            mRecyclerView.adapter = adapter
            adapter.submitList(list)
            //设置标题
            ToolbarFactory.build(titleBar.toolbar, baseActivity)
                .setBackgroundColor(colorRes(R.color.white))
                .setTitle(titleBar.toolbarTitle, "标题")
                .setBack()

            //骨架
            lifecycleScope.launch {
                veilLayout.veil()
                delay(1000)
                veilLayout.unVeil()
            }

            EventUtils.subscriber(EventBean.TYPE_1,lifecycle) {
                viewBinding.btnGet.text = it.type
                logger.info("收到请求2")
            }

        }


    }

    override fun initListener() {
        super.initListener()
       lifecycleScope.launch {
            mainActVM.channelStatusFlow.collectLatest {
                if (it.isSuccess()){
                    TToast.short("成功")
                }else{
                    TToast.short(it.message)
                }
            }

            mainActVM.loginResultFlow.collectLatest {
                if (it.isSuccess()){
                    TToast.short("成功")
                }else{
                    TToast.short(it.message)
                }
            }
        }

    }

    fun onClickView(view: View) {
        when (view.id) {
            R.id.btn_get -> mainActVM.onGet()
            R.id.btn_post -> mainActVM.onPost()
            R.id.btn_event -> {
                lifecycleScope.launch {
                    EventUtils.sendEvent(EventBean(type = EventBean.TYPE_1))
                }
            }
        }
    }

    /**
     * 权限请求
     */
    private fun permissionRequest() {
        val permissList: List<*> = listOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        //请求权限
        reqeustPermission(this, permissList as MutableList<String>, {
            logger.info("全部同意")
        }) { list: List<String?>? ->
            logger.info("不同意的权限有" + JsonUtils.toJson(list))
        }
    }


}